package com.zh.excel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.excel.entity.QrCodeTransEntity;
import com.zh.excel.service.QrCodeTransExcelService;
import com.zh.excel.util.CsvFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码交易报表控制器
 * @Author: Li.HuiNeng
 * @Create in: 2020-02-25 11:40
 * @Version: 1.0.1
 */
@Api(value = "二维码交易报表控制器", produces = "application/json", tags = {"二维码交易报表控制器"})
@RestController
@RequestMapping("/qrExcel")
public class QrCodeTransExcelController {

    @Autowired
    private QrCodeTransExcelService service;

    @ApiOperation(value = "分页查询提现日汇总数据集", tags = {"getQrCodeTransPagesDay"}, notes = "分页查询提现日汇总数据集")
    @GetMapping("/getQrCodeTransPagesDay")
    public JSONResult<List<QrCodeTransEntity>> getQrCodeTransPagesDay(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity) {
        JSONResult<List<QrCodeTransEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeTransEntity> result = this.service.selectQrCodeTransPagesDay(new Page<>(pageNo, pageSize), qrCodeTransEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "日汇总统计", tags = {"getStatisticByDay"}, notes = "日汇总统计")
    @GetMapping("/getStatisticByDay")
    public JSONResult<Object> getStatisticByDay(@ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            Map map = this.service.getStatisticByDay(qrCodeTransEntity);
            jsonResult.setData(map);
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "日导出", tags = {"exportStatisticDay"}, notes = "日汇总统计")
    @GetMapping("/exportStatisticDay")
    @ResponseBody
    public void exportStatisticDay(HttpServletResponse response,
            @ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity){
        try {
            List<QrCodeTransEntity> list = service.exportQrCodeTransPagesDay(qrCodeTransEntity);
            String[] headerCol = {"序号", "交易日期","所属机构","所属代理","所属商户","接口类型","扫码类型","支付方式","结算类型","交易总金额","交易总笔数","交易手续费"};
            //创建临时csv文件
            File tempFile = CsvFileUtil.createQrCodeTransTempFile(list, "二维码交易汇总报表", headerCol);
            //输出csv流文件，提供给浏览器下载
            CsvFileUtil.outCsvStream(response, tempFile);
            //删除临时文件
            CsvFileUtil.deleteFile(tempFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "分页查询提现月汇总数据集", tags = {"getQrCodeTransPagesMonth"}, notes = "分页查询提现月汇总数据集")
    @GetMapping("/getQrCodeTransPagesMonth")
    public JSONResult<List<QrCodeTransEntity>> getQrCodeTransPagesMonth(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity) {
        JSONResult<List<QrCodeTransEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeTransEntity> result = this.service.selectQrCodeTransPagesMonth(new Page<>(pageNo, pageSize), qrCodeTransEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "月汇总统计", tags = {"getStatisticByMonth"}, notes = "日汇总统计")
    @GetMapping("/getStatisticByMonth")
    public JSONResult<Object> getStatisticByMonth(@ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            Map map = this.service.getStatisticByMonth(qrCodeTransEntity);
            jsonResult.setData(map);
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询日清算数据集", tags = {"getSettleTransDay"}, notes = "分页查询日清算数据集")
    @GetMapping("/getSettleTransDay")
    public JSONResult<List<QrCodeTransEntity>> getSettleTransDay(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity) {
        JSONResult<List<QrCodeTransEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeTransEntity> result = this.service.selectSettleTransDay(new Page<>(pageNo, pageSize), qrCodeTransEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "日清算统计", tags = {"getSettleStatisticDay"}, notes = "日清算统计")
    @GetMapping("/getSettleStatisticDay")
    public JSONResult<Object> getSettleStatisticDay(@ApiParam(value = "二维码信息实体类", name = "qrCodeTransEntity") QrCodeTransEntity qrCodeTransEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            Map map = this.service.getSettleStatisticDay(qrCodeTransEntity);
            jsonResult.setData(map);
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }
}
